package com.baymax.test.scene.demo

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newScrollContainer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       ProviderTest
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/4/27 14:37
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class ProviderTest : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newScrollContainer {
            addButton("test provider ") {
                val bundle = context.contentResolver.call(
                    "content://com.ysten.terminal.provider".toUri(),
                    "METHOD_GET_PROPERTY",
                    "STRING",
                    bundleOf("MATHOD_PARAMS_1" to "custId")
                )
                Log.d(TAG, "onCreateView: $bundle")
                Log.d(TAG, "onCreateView: ${bundle?.getString("METHOD_RESULT")}")
            }
            addButton("test provider ") {
                val bundle = context.contentResolver.call(
                    "content://com.ysten.terminal.provider".toUri(),
                    "METHOD_GET_DEFAULT_USER",
                    "custId",
                    bundleOf("MATHOD_PARAMS_1" to "STRING")
                )
                Log.d(TAG, "onCreateView: $bundle")
                Log.d(TAG, "onCreateView: ${bundle?.getString("METHOD_RESULT")}")
            }

            addButton("test provider await ") {

                launch(Dispatchers.IO) {
                    runCatching {
                        Log.d(TAG, "onCreateView: await")
                        val bundle = context.contentResolver.call(
                            "content://com.baymax.test".toUri(),
                            "METHOD_GET_DEFAULT_USER",
                            "custId",
                            bundleOf("MATHOD_PARAMS_1" to "STRING")
                        )
                        Log.d(TAG, "onCreateView: end $bundle")
                    }.onFailure {
                        Log.w(TAG, "onCreateView: error", it)

                    }
                }
            }

        }

    }
}